<template>
  <div class="app-container">
    <!--查询表单-->
    <el-form
      :inline="true"
      :model="searchObj"
      @keyup.enter.native="getDataList()"
    >
      <el-form-item label="角色名称">
        <el-input
          v-model="searchObj.keyword"
          placeholder="角色名称"
          clearable
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="getDataList()"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetData"
          >重置</el-button
        >

        <el-button
          type="success"
          icon="el-icon-plus"
          size="mini"
          @click="addOrUpdateHandle()"
          >添 加</el-button
        >
        <el-button
          type="danger"
          class="btn-add"
          size="mini"
          :disabled="selectValue.length <= 0"
          @click="batchRemove()"
          >批量删除</el-button
        >
      </el-form-item>
    </el-form>

    <!-- 表格 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      stripe
      border
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" />

      <el-table-column
        prop="id"
        header-align="center"
        label="序号"
        width="70"
        align="center"
      />
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="角色名称"
      />
      <el-table-column
        prop="code"
        header-align="center"
        align="center"
        label="角色编码"
      />
      <el-table-column
        prop="descript"
        header-align="center"
        align="center"
        label="角色描述"
      />
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="创建时间"
        width="160"
      />
      <el-table-column label="操作" width="250" align="center">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="addOrUpdateHandle(scope.row.id)"
            >修改
          </el-button>
          <el-button
            type="danger"
            size="mini"
            @click="removeDataById(scope.row.id)"
            >删除
          </el-button>
          <el-button
            type="warning"
            size="mini"
            @click="showAssignAuth(scope.row)"
            >分配权限
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :total="total"
      :page-size="pageSize"
      style="padding: 30px 0; text-align: center"
      layout="total, prev, pager, next, sizes,jumper"
    />

    <!-- 弹窗, 新增 / 修改 -->
    <AddDialog
      v-if="dialogVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></AddDialog>
  </div>
</template>
<script>
import RoleApi from "@/api/system/role";
import AddDialog from "./add.vue";

export default {
  components: { AddDialog },
  data() {
    return {
      searchObj: {},
      listLoading: false,
      list: [],
      selectValue: [],
      pageIndex: 1,
      pageSize: 10,
      total: 0,
      dialogVisible: false,
    };
  },
  created() {
    this.getDataList();
  },
  methods: {
    //跳转分配菜单权限
    showAssignAuth(row) {
      this.$router.push(
        "/system/sysRole/assignAuth?id=" + row.id + "&roleName=" + row.roleName
      );
    },
    //复选框发生变化执行方法
    handleSelectionChange(selection) {
      this.selectValue = selection;
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    //批量删除
    batchRemove() {
      this.$confirm("此操作将永久删除该角色, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //数组
          var idList = this.selectValue.map((item) => {
            return item.id;
          });
          RoleApi.batchRemove(idList).then((response) => {
            //提示
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            //刷新
            this.getDataList();
          });
        })
        .catch(() => {});
    },
    removeDataById(id) {
      this.$confirm("此操作将永久删除该角色, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        //调用方法删除
        RoleApi.removeId({ id }).then((response) => {
          //提示
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          //刷新
          this.getDataList();
        });
      });
    },
    resetData() {
      this.searchObj = {};
      this.pageIndex = 1;
      this.getDataList();
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    getDataList() {
      this.dataListLoading = true;
      RoleApi.getDataList({
        pageindex: this.pageIndex,
        pageSize: this.pageSize,
        ...this.searchObj,
      }).then((response) => {
        this.listLoading = false;
        this.list = response.data.list;
        this.total = response.data.totalCount;
      });
    },
  },
};
</script>
